﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ExamAnalysis.Model;

namespace ExamAnalysis.Controls
{
    public partial class MultiCheckClassList : System.Web.UI.UserControl
    {
        /// <summary>
        /// 学校标识
        /// </summary>
        public int SchoolID { get; set; }
        public string ValueString
        {
            get
            {
                return ClassList.Value.ToString();
            }
        }
        public List<int> ValueList
        {
            get
            {
                return ClassList.Value.ToString().Split(',').Select(n => int.Parse(n)).ToList();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.BuildTree(ClassTree.Root);
            }
        }

        private Ext.Net.NodeCollection BuildTree(Ext.Net.NodeCollection nodes)
        {
            if (nodes == null)
            {
                nodes = new Ext.Net.NodeCollection();
            }

            Ext.Net.Node root = new Ext.Net.Node();
            nodes.Add(root);

            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var items = db.ClassInfo
                    .Where(p => p.SchoolID == this.SchoolID)
                    .OrderByDescending(p=>p.EnrollYear)
                    .ThenBy(p=>p.ClassCode)
                    .ToList();
                foreach (ClassInfo item in items)
                {
                    Ext.Net.Node node = new Ext.Net.Node();
                    node.NodeID = item.ClassID.ToString();
                    node.Text = string.Format("{1}({0})", item.ClassName, item.ClassAliasName);
                    node.Leaf = true;
                    node.Checked = false;
                    root.Children.Add(node);
                }
            }
            return nodes;
        }

    }
}